﻿namespace SmartSoft.Controls.ChildGridControls
{
    using SmartSoft;
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;

    public class SearchColumn : DataGridViewColumn
    {
        public SearchColumn()
        {
            SearchCell cell = new SearchCell();
            this.CellTemplate = cell;
            this.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
            base.SortMode = DataGridViewColumnSortMode.Automatic;
        }

        public override DataGridViewCell CellTemplate
        {
            get
            {
                return base.CellTemplate;
            }
            set
            {
                SearchCell cell = value as SearchCell;
                if ((value != null) && (cell == null))
                {
                    throw new InvalidCastException("Value provided for CellTemplate must be of type DataGridViewSearchUpDownElements.DataGridViewSearchUpDownCell or derive from it.");
                }
                base.CellTemplate = value;
            }
        }

        [Category("Search column properties"), Description("Get or set destination fieldnames to set data."), RefreshProperties(RefreshProperties.All), DefaultValue("")]
        public string DestinationFields
        {
            get
            {
                if (this.SearchCellTemplate == null)
                {
                    throw new InvalidOperationException("Operation cannot be completed because this DataGridViewColumn does not have a CellTemplate.");
                }
                return this.SearchCellTemplate.DestinationFields;
            }
            set
            {
                this.SearchCellTemplate.DestinationFields = value;
            }
        }

        [Category("Search column properties"), Description("Get or set disabled forecolor."), DefaultValue(0), RefreshProperties(RefreshProperties.All)]
        public Color DisabledForeColor
        {
            get
            {
                if (this.SearchCellTemplate == null)
                {
                    throw new InvalidOperationException("Operation cannot be completed because this DataGridViewColumn does not have a CellTemplate.");
                }
                return this.SearchCellTemplate.DisabledForeColor;
            }
            set
            {
                this.SearchCellTemplate.DisabledForeColor = value;
            }
        }

        [Description("Indicates if the value empty valid."), DefaultValue(false), Category("Search column properties"), RefreshProperties(RefreshProperties.All)]
        public bool EmptyIsValid
        {
            get
            {
                if (this.SearchCellTemplate == null)
                {
                    throw new InvalidOperationException("Operation cannot be completed because this DataGridViewColumn does not have a CellTemplate.");
                }
                return this.SearchCellTemplate.EmptyIsValid;
            }
            set
            {
                if (this.SearchCellTemplate == null)
                {
                    throw new InvalidOperationException("Operation cannot be completed because this DataGridViewColumn does not have a CellTemplate.");
                }
                this.SearchCellTemplate.EmptyIsValid = value;
                if (base.DataGridView != null)
                {
                    DataGridViewRowCollection rows = base.DataGridView.Rows;
                    int count = rows.Count;
                    for (int i = 0; i < count; i++)
                    {
                        SearchCell cell = rows.SharedRow(i).Cells[base.Index] as SearchCell;
                        if (cell != null)
                        {
                            cell.SetEmptyIsValid(i, value);
                        }
                    }
                    base.DataGridView.InvalidateColumn(base.Index);
                }
            }
        }

        [RefreshProperties(RefreshProperties.All), Category("Search column properties"), Description("Get or set the fix searched expression."), DefaultValue("")]
        public string FirstFilter
        {
            get
            {
                if (this.SearchCellTemplate == null)
                {
                    throw new InvalidOperationException("Operation cannot be completed because this DataGridViewColumn does not have a CellTemplate.");
                }
                return this.SearchCellTemplate.FirstFilter;
            }
            set
            {
                if (this.SearchCellTemplate == null)
                {
                    throw new InvalidOperationException("Operation cannot be completed because this DataGridViewColumn does not have a CellTemplate.");
                }
                this.SearchCellTemplate.FirstFilter = value;
                if (base.DataGridView != null)
                {
                    DataGridViewRowCollection rows = base.DataGridView.Rows;
                    int count = rows.Count;
                    for (int i = 0; i < count; i++)
                    {
                        SearchCell cell = rows.SharedRow(i).Cells[base.Index] as SearchCell;
                        if (cell != null)
                        {
                            cell.SetFirstFilter(i, value);
                        }
                    }
                    base.DataGridView.InvalidateColumn(base.Index);
                }
            }
        }

        [Description("Indicates if the value found valid."), DefaultValue(true), RefreshProperties(RefreshProperties.All), Category("Search column properties")]
        public bool FoundIsValid
        {
            get
            {
                if (this.SearchCellTemplate == null)
                {
                    throw new InvalidOperationException("Operation cannot be completed because this DataGridViewColumn does not have a CellTemplate.");
                }
                return this.SearchCellTemplate.FoundIsValid;
            }
            set
            {
                if (this.SearchCellTemplate == null)
                {
                    throw new InvalidOperationException("Operation cannot be completed because this DataGridViewColumn does not have a CellTemplate.");
                }
                this.SearchCellTemplate.FoundIsValid = value;
                if (base.DataGridView != null)
                {
                    DataGridViewRowCollection rows = base.DataGridView.Rows;
                    int count = rows.Count;
                    for (int i = 0; i < count; i++)
                    {
                        SearchCell cell = rows.SharedRow(i).Cells[base.Index] as SearchCell;
                        if (cell != null)
                        {
                            cell.SetFoundIsValid(i, value);
                        }
                    }
                    base.DataGridView.InvalidateColumn(base.Index);
                }
            }
        }

        private SearchCell SearchCellTemplate
        {
            get
            {
                return (SearchCell) base.CellTemplate;
            }
            set
            {
                if ((value != null) && !value.GetType().IsAssignableFrom(typeof(SearchCell)))
                {
                    throw new InvalidCastException("Must be a CustomDataGridViewSearchTextBoxCell");
                }
                base.CellTemplate = value;
            }
        }

        [Description("Get or set source fieldnames to set data."), DefaultValue(""), Category("Search column properties"), RefreshProperties(RefreshProperties.All)]
        public string SourceFields
        {
            get
            {
                if (this.SearchCellTemplate == null)
                {
                    throw new InvalidOperationException("Operation cannot be completed because this DataGridViewColumn does not have a CellTemplate.");
                }
                return this.SearchCellTemplate.SourceFields;
            }
            set
            {
                this.SearchCellTemplate.SourceFields = value;
            }
        }

        [Category("Search column properties"), Description("Get or set tablename to search on."), DefaultValue(""), RefreshProperties(RefreshProperties.All)]
        public string TableName
        {
            get
            {
                if (this.SearchCellTemplate == null)
                {
                    throw new InvalidOperationException("Operation cannot be completed because this DataGridViewColumn does not have a CellTemplate.");
                }
                return this.SearchCellTemplate.TableName;
            }
            set
            {
                if (this.SearchCellTemplate == null)
                {
                    throw new InvalidOperationException("Operation cannot be completed because this DataGridViewColumn does not have a CellTemplate.");
                }
                this.SearchCellTemplate.TableName = value;
                if (base.DataGridView != null)
                {
                    DataGridViewRowCollection rows = base.DataGridView.Rows;
                    int count = rows.Count;
                    for (int i = 0; i < count; i++)
                    {
                        SearchCell cell = rows.SharedRow(i).Cells[base.Index] as SearchCell;
                        if (cell != null)
                        {
                            cell.SetTableName(i, value);
                        }
                    }
                    base.DataGridView.InvalidateColumn(base.Index);
                }
            }
        }

        [RefreshProperties(RefreshProperties.All), Category("Search column properties"), DefaultValue(3), Description("Get or set max text length.")]
        public AppInstance.TextControlLength UserTextLength
        {
            get
            {
                if (this.SearchCellTemplate == null)
                {
                    throw new InvalidOperationException("Operation cannot be completed because this DataGridViewColumn does not have a CellTemplate.");
                }
                return this.SearchCellTemplate.UserTextLength;
            }
            set
            {
                if (this.SearchCellTemplate == null)
                {
                    throw new InvalidOperationException("Operation cannot be completed because this DataGridViewColumn does not have a CellTemplate.");
                }
                this.SearchCellTemplate.UserTextLength = value;
                if (base.DataGridView != null)
                {
                    DataGridViewRowCollection rows = base.DataGridView.Rows;
                    int count = rows.Count;
                    for (int i = 0; i < count; i++)
                    {
                        SearchCell cell = rows.SharedRow(i).Cells[base.Index] as SearchCell;
                        if (cell != null)
                        {
                            cell.SetUserTextLength(i, value);
                        }
                    }
                    base.DataGridView.InvalidateColumn(base.Index);
                }
            }
        }

        [Category("Search column properties"), Description("Get or set trim mode."), DefaultValue(2), RefreshProperties(RefreshProperties.All)]
        public AppInstance.TextControlTrimMode UserTrimMode
        {
            get
            {
                if (this.SearchCellTemplate == null)
                {
                    throw new InvalidOperationException("Operation cannot be completed because this DataGridViewColumn does not have a CellTemplate.");
                }
                return this.SearchCellTemplate.UserTrimMode;
            }
            set
            {
                if (this.SearchCellTemplate == null)
                {
                    throw new InvalidOperationException("Operation cannot be completed because this DataGridViewColumn does not have a CellTemplate.");
                }
                this.SearchCellTemplate.UserTrimMode = value;
                if (base.DataGridView != null)
                {
                    DataGridViewRowCollection rows = base.DataGridView.Rows;
                    int count = rows.Count;
                    for (int i = 0; i < count; i++)
                    {
                        SearchCell cell = rows.SharedRow(i).Cells[base.Index] as SearchCell;
                        if (cell != null)
                        {
                            cell.SetUserTrimMode(i, value);
                        }
                    }
                    base.DataGridView.InvalidateColumn(base.Index);
                }
            }
        }
    }
}

